<?php
class ChangePasswordForm extends CFormModel
{
	public $currentPass;
	public $newPass;
	public $newPass_repeat;
	
	public function rules()
	{
		return array(
			// username and password are required
			array('currentPass, newPass, newPass_repeat', 'required'),
			array('currentPass', 'isExists'),
			array('newPass', 'compare'),
			array('newPass_repeat', 'safe'),
			
		);
	}
	
	public function attributeLabels()
	{
		return array(
			'currentPass' => 'Current Password',
			'newPass' => 'New Password',
			'newPass_repeat' => 'Verify Password',
		);
	}
	
	/**
	 * Check password is exists and statify.
	 *
	 * @return returnType returnDescription
	 */
	public function isExists($attribute, $params) {
		
		$user = Yii::app()->user;
		$sql = "SELECT customerID FROM tbl_Customer WHERE customerID = :customerID AND password = :password";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindValue(':customerID', $user->getState('customerID'));
		$command->bindValue(':password', CGlobal::encrypt($this->currentPass));
		
		$customerID = $command->queryScalar();
		
		if(!$customerID)
		{
			$this->addError( 'Password change', 'Old Password is incorrect! ');
		}
		
	}
	
	public function save()
	{
		$user = Yii::app()->user;
		
		//save ani number to sql server
		$sql = "UPDATE tbl_Customer SET password = :password WHERE customerID = :customerID";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindValue(":password", CGlobal::encrypt($this->newPass));
		$command->bindValue(":customerID", $user->getState('customerID'));
		return $command->execute();

		//save ani number to oracle server
	}	
}